package com.mindainfo.groundwave.web.member.web;

import com.mindainfo.groundwave.common.api.member.param.*;
import com.mindainfo.groundwave.common.api.member.service.IRoleService;
import com.mindainfo.groundwave.common.api.member.vo.*;
import com.mindainfo.groundwave.common.bean.CommonResult;
import com.mindainfo.groundwave.common.bean.DataGrid;
import com.mindainfo.groundwave.common.bean.ResultFactory;
import com.mindainfo.groundwave.common.bean.TableData;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by MD-01 on 2017/5/17.
 */
@Controller
@RequestMapping("/sys/role")
public class RoleController {

    @Resource
    private IRoleService roleService;

    @RequestMapping("/page")
    public String page(){
        return "/member/role";
    }


    @RequestMapping("/save")
    @ResponseBody
    public CommonResult<Void> save(Integer roleId,String name,String tag,
                                   String description, Integer orderNum){


        RoleSaveParam param = new RoleSaveParam();
        param.setDescription(description);
        param.setOrderNum(orderNum);
        param.setName(name);
        param.setRoleId(roleId);
        param.setTag(tag);
        return roleService.save(param);
    }

    @RequestMapping("/queryList")
    @ResponseBody
    public TableData<RoleQueryListVo> queryList(Integer start, Integer length, String draw,String key){

        RoleQueryListParam param = new RoleQueryListParam();
        param.setKey(key);
        param.setStart(start);
        param.setNum(length);
        CommonResult<TableData<RoleQueryListVo>> result = roleService.queryList(param);
        result.getData().setDraw(draw);
        return result.getData();
    }

    @RequestMapping("/lookPermissions")
    @ResponseBody
    public CommonResult<List<RoleLookPermissionsVo>> lookPermissions(Integer roleId){
        RoleLookPermissionsParam param = new RoleLookPermissionsParam();
        param.setRoleId(roleId);
        return roleService.lookPermissions(param);
    }

    @RequestMapping("/editPermissions")
    @ResponseBody
    public CommonResult<List<RoleEditPermissionsVo>> editPermissions(Integer roleId){

        RoleEditPermissionsParam param = new RoleEditPermissionsParam();
        param.setRoleId(roleId);

        return roleService.editPermissions(param);
    }

    @RequestMapping("/setPermission")
    @ResponseBody
    public CommonResult<Void> setPermission(Integer roleId,String permissionIds){

        RoleSetPermissionParam param = new RoleSetPermissionParam();
        param.setRoleId(roleId);
        if(!StringUtils.isEmpty(permissionIds)){
            String[] idStrArray = permissionIds.split(",");
            for(String idStr : idStrArray){
                param.getPermissionIdList().add(Integer.parseInt(idStr));
            }
        }
        return roleService.setPermission(param);
    }

    @RequestMapping("/toDel")
    @ResponseBody
    public CommonResult<Void> toDel(Integer roleId){
        RoleToDelParam param = new RoleToDelParam();
        param.setRoleId(roleId);
        return roleService.toDel(param);
    }

}
